package com.jingdianjichi.practice.server.context;

import org.springframework.util.CollectionUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * ClassName:LoginContextHolder
 * Package:com.jingdianjichi.subject.application.context
 * Description:
 *
 * @Author fst
 * @Create 2025/1/3 21:20
 * @Version 1.0
 */
public class LoginContextHolder
{
    private static final  InheritableThreadLocal<Map<String,Object>> THREAD_LOCAL
            = new InheritableThreadLocal<>();

    public static  void set(String key, Object val)
    {
        Map<String, Object> threadLocalMap = getThreadLocalMap();
        threadLocalMap.put(key,val);
    }

    public  static  Object get (String key)
    {
        Map<String, Object> stringObjectMap = getThreadLocalMap();
        return stringObjectMap.get(key);
    }


    public static  Map<String,Object> getThreadLocalMap()
    {
        Map<String,Object> map = THREAD_LOCAL.get();
        if(CollectionUtils.isEmpty(map))
        {
            map=new ConcurrentHashMap<>();
            THREAD_LOCAL.set(map);
        }
        return map;
    }

    public static void remove()
    {
        THREAD_LOCAL.remove();
    }

    public static String getLoginId()
    {
        return (String)get("loginId");
    }
}
